<input type="hidden" name="memorial.id" value="#(memorial.id??)" />
<div class="layui-form-item">
	<label class="layui-form-label">纪念标题</label>
	<div class="layui-input-block">
		<input type="text" name="memorial.memoriaTitle" value="#(memorial.memoria_title??)" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">纪念时间</label>
	<div class="layui-input-block layui-input-wrap">
		<div class="layui-input-prefix">
			<i class="layui-icon layui-icon-date"></i>
		</div>
		<input type="text" name="memorial.memorialDayDate" value="#(memorial.memorial_day_date??)" lay-verify="required" id="memorial" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input"/>
	</div>
</div>

<div class="layui-form-item">

	<label class="layui-form-label">是否农历</label>
	<div class="layui-input-block">
		<input type="checkbox" name="memorial.isLunar" value="#(memorial.is_lunar??)" id="is_lunar" lay-skin="switch" lay-filter="switchLunar" title="&nbsp;是&nbsp;|&nbsp;否&nbsp;"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">消息提示</label>
	<div class="layui-input-block">
		<input type="checkbox" name="memorial.isMsg" value="#(memorial.is_msg??)" id="is_msg" lay-skin="switch" lay-filter="switchMsg" title="&nbsp;打开&nbsp;|&nbsp;关闭&nbsp;"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">信息</label>
	<div class="layui-input-block">
		<input type="text" name="memorial.note" value="#(memorial.note??)" placeholder="请输入" autocomplete="off" class="layui-input"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">执行次数</label>
	<div class="layui-input-block">
		<input type="number" name="memorial.openCount" value="#(memorial.open_count??)" lay-verify="required" placeholder="请输入" min="0" max="10000" class="layui-input"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">日期分隔</label>
    <input type="hidden" name="memorial.send_interval_day" value="#(memorial.send_interval_day??)" />
	<div class="layui-input-block">
		<input type="checkbox" name="sendIntervalDay" value="0" title="当天"/>
		<input type="checkbox" name="sendIntervalDay" value="-1" title="提前1天"/>
		<input type="checkbox" name="sendIntervalDay" value="-3" title="提前3天"/>
		<input type="checkbox" name="sendIntervalDay" value="-5" title="提前5天"/>
		<input type="checkbox" name="sendIntervalDay" value="-7" title="提前7天"/>
		<input type="checkbox" name="sendIntervalDay" value="-17" title="提前14天"/>
		<input type="checkbox" name="sendIntervalDay" value="-30" title="提前30天"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">提醒时间</label>
    <input type="hidden" name="memorial.send_interval_time" value="#(memorial.send_interval_time??)" />
	<div class="layui-input-block">
		<input type="checkbox" name="sendIntervalTime" value="0630" title="6:30"/>
		<input type="checkbox" name="sendIntervalTime" value="0830" title="8:30"/>
		<input type="checkbox" name="sendIntervalTime" value="1030" title="10:30"/>
		<input type="checkbox" name="sendIntervalTime" value="1230" title="12:30"/>
		<input type="checkbox" name="sendIntervalTime" value="1430" title="14:30"/>
		<input type="checkbox" name="sendIntervalTime" value="1630" title="16:30"/>
		<input type="checkbox" name="sendIntervalTime" value="1830" title="18:30"/>
		<input type="checkbox" name="sendIntervalTime" value="2030" title="20:30"/>
		<input type="checkbox" name="sendIntervalTime" value="2230" title="22:30"/>
		<input type="checkbox" name="sendIntervalTime" value="2330" title="23:30"/>
		<input type="checkbox" name="sendIntervalTime" value="2359" title="23:59"/>
	</div>
</div>

<div class="layui-form-item">
	<label class="layui-form-label">重要程度</label>
	<div class="layui-input-block">
		<input type="hidden" name="memorial.important" value="#(memorial.important??)"/>
		<div id="ID-rate-demo"></div>
	</div>
</div>

<div class="layui-form-item">
	<div class="layui-input-block">
		<button type="button" class="layui-btn" lay-submit lay-filter="demo-login">立即提交</button>
	</div>
</div>


<script>
    layui.use(['form', 'layer','jquery','rate','laydate'], function(){
		let form = layui.form;
		let layer = layui.layer;
		let $ = layui.jquery;
		var rate = layui.rate;
		var laydate = layui.laydate;

        let isLunar = $("#memorial_form input[name='memorial.isLunar']").val();
        let isMsg = $("#memorial_form input[name='memorial.isMsg']").val();
        let important = $("#memorial_form input[name='memorial.important']").val() ?? 0;
        const intervalDay = $("#memorial_form input[name='memorial.send_interval_day']").val().split(",");
        const intervalTime = $("#memorial_form input[name='memorial.send_interval_time']").val().split(",");
        if (intervalDay.length > 1){
            $("#memorial_form input[name='sendIntervalDay']").each(function() {
                for (let value of intervalDay) {
                    if ($(this).val() == value){
                        $(this).prop("checked", true);
                    }
                }
            });
        }
        if(intervalTime.length > 1){
            $("#memorial_form input[name='sendIntervalTime']").each(function() {
                for (let value of intervalTime) {
                    if ($(this).val() == value){
                        $(this).prop("checked", true);
                    }
                }
            });
        }
        if (isLunar == 1){
            $('#is_lunar').prop("checked", true);
        }
        if(isMsg == 1){
            $('#is_msg').prop("checked", true);
        }
		laydate.render({
			elem: '#memorial'
		});

        $("#cb1").prop("checked",true);
		rate.render({
			elem: '#ID-rate-demo',
			choose: function(value){
				$("#memorial_form input[name='memorial.important']").val(value);
			},
            value: important
		});

		form.on('switch(switchLunar)', function(data){
			$("#memorial_form input[name='memorial.isLunar']").val(this.checked ? '1' : '0');
		});

		form.on('switch(switchMsg)', function(data){
			$("#memorial_form input[name='memorial.isMsg']").val(this.checked ? '1' : '0');
		});

		// 提交事件
		form.on('submit(demo-login)', function(data){

			let send_interval_day_vals =  [];
			$("input[name='sendIntervalDay']:checked").each(function() {
				send_interval_day_vals.push($(this).val());
			});

			let send_interval_time_vals =  [];
			$("input[name='sendIntervalTime']:checked").each(function() {
				send_interval_time_vals.push($(this).val());
			});

			let field = data.field;

			field.sendIntervalDayVals = send_interval_day_vals;
			field.sendIntervalTimeVals = send_interval_time_vals;

			$.post($("#memorial_form").attr("action"),field,function(result){
				if(result.code===200){
					var index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe 层的索引
					parent.layer.close(index); // 再执行关闭
					setTimeout(function () {
						parent.location.reload();
					},1000)
				}else{
					layer.msg("插入失败！"+result.msg);
				}
			});
			return false; // 阻止默认 form 跳转
		});
	});
</script>